package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.ruoyi.business.domain.AppCustomerBill;
import com.ruoyi.business.enums.CustomerBillMoneyType;
import com.ruoyi.business.mapper.AppCustomerBillMapper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AppCustomerMapper;
import com.ruoyi.business.domain.AppCustomer;
import com.ruoyi.business.service.IAppCustomerService;

/**
 * 客户信息Service业务层处理
 * 
 * @author CuiYingJie
 * @date 2020-11-30
 */
@Service
public class AppCustomerServiceImpl implements IAppCustomerService 
{
    private final AppCustomerMapper appCustomerMapper;
    private final AppCustomerBillMapper appCustomerBillMapper;

    public AppCustomerServiceImpl(AppCustomerMapper appCustomerMapper, AppCustomerBillMapper appCustomerBillMapper) {
        this.appCustomerMapper = appCustomerMapper;
        this.appCustomerBillMapper = appCustomerBillMapper;
    }

    /**
     * 查询客户信息
     * 
     * @param id 客户信息ID
     * @return 客户信息
     */
    @Override
    public AppCustomer selectAppCustomerById(Integer id)
    {
        return appCustomerMapper.selectAppCustomerById(id);
    }

    /**
     * 查询客户信息列表
     * 
     * @param appCustomer 客户信息
     * @return 客户信息
     */
    @Override
    public List<AppCustomer> selectAppCustomerList(AppCustomer appCustomer)
    {
        return appCustomerMapper.selectAppCustomerList(appCustomer);
    }

    /**
     * 新增客户信息
     * 
     * @param appCustomer 客户信息
     * @return 结果
     */
    @Override
    public int insertAppCustomer(AppCustomer appCustomer)
    {
        return appCustomerMapper.insertAppCustomer(appCustomer);
    }

    /**
     * 修改客户信息
     * 
     * @param appCustomer 客户信息
     * @return 结果
     */
    @Override
    public int updateAppCustomer(AppCustomer appCustomer)
    {
        return appCustomerMapper.updateAppCustomer(appCustomer);
    }

    /**
     * 批量删除客户信息
     * 
     * @param ids 需要删除的客户信息ID
     * @return 结果
     */
    @Override
    public int deleteAppCustomerByIds(Integer[] ids)
    {
        return appCustomerMapper.deleteAppCustomerByIds(ids);
    }

    /**
     * 删除客户信息信息
     * 
     * @param id 客户信息ID
     * @return 结果
     */
    @Override
    public int deleteAppCustomerById(Integer id)
    {
        return appCustomerMapper.deleteAppCustomerById(id);
    }

    /**
     * 根据用户id修改余额
     * @param data 参数
     * @return 处理结果
     */
    @Override
    public int updateBalanceByUserId(Map<String, String> data) {
        String type = data.get("type");
        int updateResult;
        switch (type){
            case "1" :
                updateResult = appCustomerMapper.customerBalanceAdd(data);
                if (updateResult == 1){
                    AppCustomerBill param = new AppCustomerBill();
                    param.setCustomerId(Integer.valueOf(data.get("selectValue")));
                    param.setMoney(BigDecimal.valueOf(Long.parseLong(data.get("balanceNumber"))));
                    param.setMoneyType(CustomerBillMoneyType.RECHARGE.getCode());
                    param.setMsg("后台管理员手动充值");
                    appCustomerBillMapper.insertAppCustomerBill(param);
                }
                return updateResult;
            case "2" :
                updateResult = appCustomerMapper.customerBalanceSub(data);
                if (updateResult == 1){
                    AppCustomerBill param = new AppCustomerBill();
                    param.setCustomerId(Integer.valueOf(data.get("selectValue")));
                    param.setMoney(BigDecimal.valueOf(Long.parseLong(data.get("balanceNumber"))));
                    param.setMoneyType(CustomerBillMoneyType.COST.getCode());
                    param.setMsg("后台管理员手动扣费");
                    appCustomerBillMapper.insertAppCustomerBill(param);
                }
                return updateResult;
            default:
                return 0;
        }
    }

    /**
     * 封禁用户
     */
    @Override
    public int limitCustomer(Map<String, String> data) {
        String customerId = data.get("id");
        AppCustomer appCustomer = appCustomerMapper.selectAppCustomerById(Integer.parseInt(customerId));
        if (StringUtils.isEmpty(customerId) || appCustomer.getLimitStatus() == 1){
            return 0;
        }
        return appCustomerMapper.limitCustomer(data);
    }

    @Override
    public Integer countCustomerNum() {
        return appCustomerMapper.countCustomerNum();
    }

    @Override
    public Double countCustomerPriceNum() {
        return appCustomerMapper.countCustomerPriceNum();
    }
}
